package net.aiform.template.action;

import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.aiform.bean.Constant;
import net.aiform.security.pojo.FormAccessController;
import net.aiform.template.pojo.Field;
import net.aiform.template.pojo.Template;
import net.aiform.template.service.TemplateService;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

import ext.cjt.CommonMethods;

public final class SaveTemplateAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private TemplateService templateService;
	private HttpServletRequest req;
	private Template template;
	private String[] code;
	private String[] name;
	private String[] category;
	private String[] length;
	private String[] defaultValue;
	private String[] exampleValue;
	private String[] restrain;
	private String[] restrainRemark;
	private String[] remark;
	private String template_recordAC;
	private String template_resultAC;
	private String template_statisticsAC;
	private String arg1;

	@SuppressWarnings("unchecked")
	public String execute() {
		List<Map<String, Object>> params = (List<Map<String, Object>>) req.getAttribute(Constant.PARAMS);
		Template t = initTemplate(params);
		this.templateService.save(t);
		return Action.SUCCESS;
	}

	private Template initTemplate(List<Map<String, Object>> params) {
		Template st = this.templateService.get(this.template.getId());
		st.setFormAccessControllers(new HashSet<FormAccessController>());
		st.getFormAccessControllers().add(getFAC(st, this.template_recordAC, Template.AC_RECORD));
		st.getFormAccessControllers().add(getFAC(st, this.template_resultAC, Template.AC_RESULT));
		st.getFormAccessControllers().add(getFAC(st, this.template_statisticsAC, Template.AC_STATISTICS));
		initField(st, params);
		st.setCode(this.template.getCode());
		st.setName(this.template.getName());
		st.setCategory(this.template.getCategory());
		st.setRecordStart(this.template.getRecordStart());
		st.setRecordEnd(this.template.getRecordEnd());
		st.setStatisticsStart(this.template.getStatisticsStart());
		st.setStatisticsEnd(this.template.getStatisticsEnd());
		st.setHeader(this.template.getHeader());
		st.setFooter(this.template.getFooter());
		st.setFields(this.template.getFields());
		st.setFieldsAmount(this.template.getFieldsAmount());
		return st;
	}

	private void initField(Template t, List<Map<String, Object>> params) {
		Field field;
		this.template.setFields(new HashSet<Field>());
		for (int i = 0; i < params.size(); i++) {
			Map<String, Object> f = params.get(i);
			field = new Field();
			field.setTemplate(t);
			field.setCode((String) f.get("code"));
			field.setName((String) f.get("name"));
			field.setCategory((String) f.get("category"));
			field.setPosition(i + 1);
			Integer len = CommonMethods.getInt((String) f.get("length"));
			field.setLength(0 == len ? 32 : len);
			field.setDefaultValue((String) f.get("defaultValue"));
			field.setExampleValue((String) f.get("exampleValue"));
			field.setRestrain((String) f.get("restrain"));
			field.setRestrainRemark((String) f.get("restrainRemark"));
			field.setRemark((String) f.get("remark"));
			field.setHidden("on".equalsIgnoreCase((String) f.get("hidden")));
			this.template.getFields().add(field);
		}
		this.template.setFieldsAmount(params.size());
	}

	private FormAccessController getFAC(Template t, String category, String code) {
		FormAccessController fac = new FormAccessController();
		fac.setCategory(category);
		fac.setCode(code);
		fac.setTemplate(t);
		return fac;
	}

	public SaveTemplateAction() {
		super();
	}

	public SaveTemplateAction(TemplateService templateService) {
		this.templateService = templateService;
	}

	public void prepare() throws Exception {

	}

	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public HttpServletRequest getReq() {
		return req;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	public String[] getCode() {
		return code;
	}

	public void setCode(String[] code) {
		this.code = code;
	}

	public String[] getName() {
		return name;
	}

	public void setName(String[] name) {
		this.name = name;
	}

	public String[] getCategory() {
		return category;
	}

	public void setCategory(String[] category) {
		this.category = category;
	}

	public String[] getLength() {
		return length;
	}

	public void setLength(String[] length) {
		this.length = length;
	}

	public String[] getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String[] defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String[] getExampleValue() {
		return exampleValue;
	}

	public void setExampleValue(String[] exampleValue) {
		this.exampleValue = exampleValue;
	}

	public String[] getRestrain() {
		return restrain;
	}

	public void setRestrain(String[] restrain) {
		this.restrain = restrain;
	}

	public String[] getRemark() {
		return remark;
	}

	public void setRemark(String[] remark) {
		this.remark = remark;
	}

	public String getTemplate_recordAC() {
		return template_recordAC;
	}

	public void setTemplate_recordAC(String templateRecordAC) {
		template_recordAC = templateRecordAC;
	}

	public String getTemplate_resultAC() {
		return template_resultAC;
	}

	public void setTemplate_resultAC(String templateResultAC) {
		template_resultAC = templateResultAC;
	}

	public String getTemplate_statisticsAC() {
		return template_statisticsAC;
	}

	public void setTemplate_statisticsAC(String templateStatisticsAC) {
		template_statisticsAC = templateStatisticsAC;
	}

	public String[] getRestrainRemark() {
		return restrainRemark;
	}

	public void setRestrainRemark(String[] restrainRemark) {
		this.restrainRemark = restrainRemark;
	}
}
